
app.controller('settingscontroller', ['$scope', '$parse', '$http', function($scope, $parse, $http) 
{
	$scope.isComponentLoading = true;
	
	/**
	* Reset the mapping
	*/
	$scope.resetMappingCommand = function() 
	{
		console.info("resetMappingCommand - called"); 
		
		$scope.loadingResetMappings = true;
		$scope.canResetMappings = false;
		
		$http.post($scope.servicebase, { "service" : "resetMappings"}).
		success(function(data, status) 
		{
			console.info("resetMappingCommand - call success"); 
			$scope.loadingResetMappings = false;
			$scope.canResetMappings = true;
			$scope.status = status;
			$scope.data = data;
			$scope.result = data;
			
			if($scope.data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			else
			{
				$scope.setMessageCommand("La base de données a été réinitialisée.","success");
			}
		})
		.
		error(function(data, status)
		{
			console.error("resetMappingCommand - call failed"); 	
			$scope.loadingResetMappings = false;
			$scope.data = data || "Request failed";
			$scope.status = status;	
			$scope.setMessageCommand("Echec lors de la réinitialisation de la base de données.","danger");
			console.error("resetMappingCommand - data : " + $scope.data);			
		});
	};
	
	/**
	* Get configuration 
	*/
	$scope.getConfigurationCommand = function() 
	{
		console.info("getConfigurationCommand - called"); 
		$scope.loadingConfiguration = true;
		$http.post($scope.servicebase, { "service" : "getConfiguration"}).
		success(function(data, status) 
		{
			console.info("getConfigurationCommand - call success"); 
			$scope.loadingConfiguration = false;
			$scope.status = status;
			$scope.data = data;
			$scope.result = data;
			if($scope.data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			else
			{
				$scope.parameters = $scope.data.response;
			}
		})
		.
		error(function(data, status) 
		{
			console.error("getConfigurationCommand - call failed"); 	
			$scope.loadingConfiguration = false;
			$scope.data = data || "Request failed";
			$scope.status = status;	
			console.error("getConfigurationCommand - data : " + $scope.data);
		});
	};
	
	/**
	* Get packages 
	*/
	$scope.getPackagesCommand = function() 
	{
		console.info("getPackagesCommand - called"); 
		
		$scope.loadingConfiguration = true;
		
		$scope.url = '../../Core/CDF91680CoreService/Services/getPackagesClient.php';
		
		console.info("getPackagesCommand - call getConfigurationClient"); 
		
		$http.post($scope.url).
		success(function(data, status) 
		{
			console.info("getPackagesCommand - call success"); 
			$scope.loadingConfiguration = false;
			$scope.status = status;
			$scope.data = data;
			$scope.result = data;
			if($scope.data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			else
			{
				$scope.packages = $scope.data.response;
			}
		})
		.
		error(function(data, status)
		{
			console.error("getPackagesCommand - call failed"); 	
			$scope.loadingConfiguration = false;
			$scope.data = data || "Request failed";
			$scope.status = status;	
			$scope.setMessageCommand("Erreur lors de la récupération des packages.","danger");
			console.error("getPackagesCommand - data : " + $scope.data);			
		});
	};
	
	/**
	* get parameters files
	*/
	$scope.getParametersFilesCommand = function() 
	{
		console.info("getParametersFilesCommand - called"); 
		$http.post($scope.servicebase, { "service" : "getParametersFiles"}).
		success(function(data, status) 
		{
			console.info("getParametersFilesCommand - call success"); 
			$scope.status = status;
			$scope.data = data;
			$scope.result = data;
			if($scope.data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			else
			{
				$scope.filesParameters = $scope.data.response;
			}
		})
		.
		error(function(data, status) 
		{
			console.error("getParametersFilesCommand - call failed"); 	
			$scope.data = data || "Request failed";
			$scope.status = status;
			console.error("getParametersFilesCommand - data : " + $scope.data);		
		});
	};
	
	/*  
		Prepare to create a package
	*/
	$scope.prepareAddPackageCommand = function()
	{
		$("#addPackage").modal('show');
	}
	
	/*
		Load file parameter
	*/
	$scope.loadFileParameterCommand = function()
	{
		console.info("loadFileParameterCommand - called"); 
		
		$scope.url = '../../Core/CDF91680CoreService/Services/loadFileParameterClient.php';
		
		console.info("loadFileParameterCommand - call loadFileParameterClient"); 
		
		$http.post($scope.url, { "fileParameter" : $scope.fileParameter}).
		success(function(data, status) 
		{
			console.info("loadFileParameterCommand - call success"); 
			$scope.status = status;
			$scope.data = data;
			$scope.result = data;
			
			if($scope.data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			else
			{
				$scope.setMessageCommand("Changement de configuration effectué.","success");
				$scope.getConfigurationCommand();
			}
		})
		.
		error(function(data, status)
		{
			console.error("loadFileParameterCommand - call failed"); 	
			$scope.data = data || "Request failed";
			$scope.status = status;	
			$scope.setMessageCommand("Echec lors du changement de configuration.","danger");
			console.error("loadFileParameterCommand - data : " + $scope.data);			
		});
	}
	
	/*
		Upload scripts command
	*/
	$scope.uploadScriptsCommand = function(files)
	{
		console.info("uploadScriptsCommand - called"); 
		
		$scope.url = '../../Core/CDF91680CoreService/Services/uploadScriptsClient.php';
		
		for (index = 0; index < files.length; ++index)
		{
			console.info("uploadScriptsCommand - call uploadScriptsClient with index : " + index); 
			var fd = new FormData();
			fd.append("file", files[index]);
			$http.post($scope.url,
			fd,
			{ 
				withCredentials: true, headers: {'Content-Type': undefined }, transformRequest: angular.identity 
			}
			).
			success(function(data, status) 
			{
				console.info("uploadScriptsCommand - call success"); 
				
				if(data.isFailed == true)
				{
					$scope.setMessageCommand(data.exception, 'danger');
				}
				else
				{
					$scope.setMessageCommand("Script copié.","success");
				}
			})
			.
			error(function(data, status) 
			{
				console.error("uploadScriptsCommand - call failed"); 	
				$scope.canCreateDump = true;	
				$scope.data = data || "Request failed";
				$scope.status = status;	
				$scope.setMessageCommand("Echec lors de la copie du script.","danger");
				console.error("uploadScriptsCommand - data : " + $scope.data);						
			});
		}
	}
	
	/*
		Add package command
	*/
	$scope.addPackageCommand = function()
	{
		console.info("addPackageCommand - called"); 
		
		$scope.url = '../../Core/CDF91680CoreService/Services/addPackageClient.php';
		
		console.info("addPackageCommand - call addPackageClient"); 
		
		$http.post($scope.url, { "newPackageVersion" : $scope.newPackageVersion}).
		success(function(data, status) 
		{
			console.info("addPackageCommand - call success"); 
			$scope.status = status;
			$scope.data = data;
			$scope.result = data;
			
			if($scope.data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			else
			{
				$scope.canCreateDump = true;
				$scope.getPackagesCommand();
				$scope.setMessageCommand("Package créé.","success");
			}
		})
		.
		error(function(data, status)
		{
			console.error("addPackageCommand - call failed"); 	
			$scope.canCreateDump = true;	
			$scope.data = data || "Request failed";
			$scope.status = status;	
			$scope.setMessageCommand("Echec lors de la création du package.","danger");
			console.error("addPackageCommand - data : " + $scope.data);		
		});
	}
	
	/* 
		package selection changed
	*/
	$scope.selectedPackageChangedCommand = function()
	{
		if($scope.selectedpackage != null || $scope.selectedpackage != '')
		{
			$scope.isDeployable = true;
		}
	}
	
	/*
		deploy a package
	*/
	$scope.deployCommand = function()
	{
		console.info("deployCommand - called"); 
		
		$scope.url = '../../Core/CDF91680CoreService/Services/deployPackageClient.php';
		
		console.info("deployCommand - call deployPackageClient"); 
		
		$http.post($scope.url, { "selectedpackage" : $scope.selectedpackage}).
		success(function(data, status) 
		{
			console.info("deployCommand - call success"); 
			$scope.status = status;
			$scope.data = data;
			$scope.result = data;
			
			if($scope.data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			else
			{
				$scope.setMessageCommand("Package déployé.","success");
			}
		})
		.
		error(function(data, status)
		{
			console.error("deployCommand - call failed"); 	
			$scope.canCreateDump = true;	
			$scope.data = data || "Request failed";
			$scope.status = status;	
			$scope.setMessageCommand("Echec lors du déploiement du package.","danger");
			console.error("deployCommand - data : " + $scope.data);		
		});
	}
	
	/*
		Create a backup manually
	*/
	$scope.createBackupCommand = function()
	{
		console.info("createBackupCommand - called"); 
		$scope.canCreateDump = false;
		
		
		$scope.url = '../../Core/CDF91680CoreService/Services/createBackupClient.php';
		
		console.info("createBackupCommand - call createBackupClient"); 
		
		$http.post($scope.url).
		success(function(data, status) 
		{
			console.info("createBackupCommand - call success"); 
			$scope.status = status;
			$scope.data = data;
			$scope.result = data;
			
			if($scope.data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			else
			{
				$scope.canCreateDump = true;
				$scope.setMessageCommand("Sauvegarde effectuée.","success");
			}
		})
		.
		error(function(data, status)
		{
			console.error("createBackupCommand - call failed"); 	
			$scope.canCreateDump = true;	
			$scope.data = data || "Request failed";
			$scope.status = status;	
			$scope.setMessageCommand("Echec lors de la création de la sauvegarde.","danger");
			console.error("createBackupCommand - data : " + $scope.data);		
		});
	}
	
	/*
		Define refresh accounts process
	*/
	$scope.loadSettingComponentCommand = function() 
	{
		console.info("loadSettingComponentCommand - called");
		
		console.info("Set variables");
		$("#jqxProgressBar").jqxProgressBar({ width: 250, height: 25, value: 0 });
		$scope.canResetMappings = true;
		$scope.canCreateDump = true;
		$scope.loadingConfiguration = false;
		$scope.parameters = [];
		$scope.packages = [];
		$scope.filesParameters = [];
		$scope.fileParameter = '';
		$scope.isDeployable = false;
		
		$("#jqxProgressBar").jqxProgressBar({ value: 66 });
		
		console.info("loadSettingComponentCommand called");
		$http.post($scope.servicebase, { "service" : "getTranslations"}).
		success(function(data, status) 
		{
			console.info("loadSettingComponentCommand - call success"); 
			$scope.status = status;
			$scope.data = data;
			$scope.result = data;
			if(data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			else
			{
				for (key in data.response) {
					if (data.response.hasOwnProperty(key)) 
					{
						var model = $parse(key);
						model.assign($scope, data.response[key]);
						if (!$scope.$$phase && !$scope.$root.$$phase){
							$scope.$apply();
						}
					}
				}   
			}
		})
		.
		error(function(data, status) 
		{	
			console.error("loadSettingComponentCommand - call failed"); 	
			$scope.data = data || "Request failed";
			$scope.status = status;
			console.error("loadSettingComponentCommand - data : " + $scope.data);		
		});
		
		$scope.getParametersFilesCommand();
		$scope.getConfigurationCommand();
		$scope.isComponentLoading = false;
		
		$("#jqxProgressBar").jqxProgressBar({ value: 100 });
	}
	
	$scope.loadSettingComponentCommand();
}]);